package de.mafr.jsystest.ws.json;

import net.sf.json.JSON;
import net.sf.json.JSONException;
import net.sf.json.JSONSerializer;

import org.apache.commons.httpclient.HttpMethodBase;

import de.mafr.jsystest.SystemTestException;
import de.mafr.jsystest.ws.HttpUtils;
import de.mafr.jsystest.ws.Response;
import de.mafr.jsystest.ws.ResponseFactory;


public class JsonResponseFactory implements ResponseFactory<JSON> {

	public Response<JSON> create(HttpMethodBase method) {
		return new Response<JSON>(method.getStatusCode(), createJson(HttpUtils.getBody(method)));
	}
	
	private static JSON createJson(String str) {
		try {
			return JSONSerializer.toJSON(str);
		} catch (JSONException e) {
			throw new SystemTestException("Invalid JSON string: " + str, e);
		}
	}
}
